<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@ taglib uri="http://www.springframework.org/security/tags" prefix="sec"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script src="${pageContext.request.contextPath}/static/js/jquery/jquery-2.1.3.js"></script>
<script src="${pageContext.request.contextPath}/static/js/jquery/jquery-ui.js"></script>
<link rel="stylesheet" href="${pageContext.request.contextPath}/static/css/jquery/jquery-ui.css">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Edit agent</title>
</head>
<script type="text/javascript">
function split( val ) {
      return val.split( /,\s*/ );
}

function extractLast( term ) {
      return split( term ).pop();
}

$( document ).ready(function() {
 
    $( "#categories" )
      // don't navigate away from the field on tab when selecting an item
      .bind( "keydown", function( event ) {
        if ( event.keyCode === $.ui.keyCode.TAB &&
            $( this ).autocomplete( "instance" ).menu.active ) {
          event.preventDefault();
        }
      })
      .autocomplete({
        source: function( request, response ) {
          $.getJSON( "${pageContext.request.contextPath}/categories/all", {
            term: extractLast( request.term )
          }, response );
        },
        search: function() {
          // custom minLength
          var term = extractLast( this.value );
          if ( term.length < 2 ) {
            return false;
          }
        },
        focus: function() {
          // prevent value inserted on focus
          return false;
        },
        select: function( event, ui ) {
          var terms = split( this.value );
          // remove the current input
          terms.pop();
          // add the selected item
          terms.push( ui.item.value );
          // add placeholder to get the comma-and-space at the end
          terms.push( "" );
          this.value = terms.join( ", " );
          return false;
        }
      });
    
  });
</script>
<body>
	
	<form:form action="${pageContext.request.contextPath}/adverts/editAdvert" modelAttribute="editedAdvert" method="POST" enctype="multipart/form-data">
	<table>
		<form:hidden path="id"/>
		<tr>
			<td><form:label path="title">Title</form:label></td>
			<td><form:input placeholder="title" path="title" required="true" /></td>
			<form:errors path="title"></form:errors>
		</tr>
		<tr>
			<td><form:label path="abstractText">Abstract text</form:label></td>
			<td><form:input type="text" path="abstractText" disabled="false" /></td>
		</tr>
		<tr>
			<form:label path="categoryNames">Categories (csv)</form:label>
			<form:input type="text" placeholder="Categories" path="categoryNames" required="true" id="categories"/> 
		</tr>
		<tr>
			<td><form:label path="address">Address</form:label></td>
			<td><form:input type="text" placeholder="address" path="address" disabled="true"/>
			<form:errors path="address"></form:errors>
		</tr>
		<tr>
			<td><form:label path="advertiser.username">Advertiser</form:label></td>
			<td><form:input type="text" placeholder="advertiser.username" path="advertiser.username" disabled="true"/></td>
			<form:errors path="advertiser"></form:errors>
		</tr>
		<tr>
			<td><form:label path="phoneNumber">Phone Number</form:label></td>
			<td><form:input placeholder="phoneNumber" path="phoneNumber" type="phoneNumber" required="true" disabled="true"/></td>
			<form:errors path="phoneNumber"></form:errors>
		</tr>
		<tr>
			<td><form:label path="file">File to upload:</form:label></td>
			<td>
				<input type="file" name="file" accept="application/pdf"></input>
				<form:errors path="file"></form:errors>
			</td>
		</tr>
		<tr>
			<td><input type="submit" value="Confirm edit"></td>
		</tr>
		</table>
	</form:form>
	
	<br><br>
	<jsp:include page="addNewCategory.jsp" />
</body>
</html>